package com.example.gobang.game;


import com.example.gobang.model.User;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

@Component
public class RoomManger {

    //房间表  房间id <--> 一个房间
    private ConcurrentHashMap<String,Room> rooms = new ConcurrentHashMap<>();
    //玩家id <--> 房间id 映射表
    private ConcurrentHashMap<Integer,String> userIdToRoomId = new ConcurrentHashMap<>();

    public void add(Room room, Integer user1Id,Integer user2Id){
        rooms.put(room.getRoomId(),room);
        userIdToRoomId.put(user1Id,room.getRoomId());
        userIdToRoomId.put(user2Id,room.getRoomId());
    }

    public void remove(Room room, Integer user1Id,Integer user2Id){
        rooms.remove(room.getRoomId());
        userIdToRoomId.remove(user1Id);
        userIdToRoomId.remove(user2Id);
    }

    public Room getByRoomId(String roomId){
        return rooms.get(roomId);
    }

    public Room getRoomByUserId(int userId){
        String roomId = userIdToRoomId.get(userId);
        if (roomId == null){
            return null;
        }
        Room room = rooms.get(roomId);
        return room;
    }
}
